package com.example.statistics.ui.product

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.ProductBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.imageDisplay
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.example.statistics.ex.contentText
import kotlinx.android.synthetic.main.activity_product_info.*
import org.jetbrains.anko.sdk27.coroutines.onClick

class ProductInfoActivity : BaseActivity() {

    override val title: String
        get() = "商品详情"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_info)
        val productId = intent.getIntExtra(Constants.ID, 0)
        topBarRightText("编辑") {
            activityForResult<ProductEditActivity>(Constants.ID to productId) {
                activityFinish()
            }
        }

        httpGet<ProductBean>(UrlConfig.PRODUCT_INFO_DATA, config = {
            listParams = listOf("id" to productId)
        }) { bean ->
            if (bean != null) {
                tv_title.contentText = bean.factoryNo + bean.productName

                tv_remark.contentText = bean.remark
                val imageList = bean.imageList?.map { UrlConfig.BASE_URL + it.path }
                if (!imageList.isNullOrEmpty()) {
                    banner.setViewUrls(imageList.toList())
                    banner.setOnBannerItemClickListener { index ->
                        imageDisplay(imageList, index)
                    }
                    banner.onClick { _ ->
                        imageDisplay(imageList)
                    }
                }
            }
        }
    }
}